<?php
// +----------------------------------------------------------------------
// | Category 类型服务
// +----------------------------------------------------------------------
// | 主要服务于Category数据处理
// +----------------------------------------------------------------------
// | 错误编码头 240xxx
// +----------------------------------------------------------------------

namespace common\service;


use common\model\StepCategoryAttributeModel;
use think\module\Module;

class StepCategoryAttributeService
{

    /**
     * 查询分类的属性列表
     * @param $param
     * @return array
     */
    public function getAttributeList($param)
    {
        $filter = $param['filter'] ?? [];
        $page = $param['page'] ?? [0, 20];

        $fieldsStepCategoryAttr = array_keys(Module::$moduleDictData['field_index_by_code']['step_category_attribute']['fixed']);
        $fieldsStepCategoryAttr = array_map(function ($item) {
            $item = "step_category_attribute." . $item;
            return $item;
        }, $fieldsStepCategoryAttr);

        $fieldsAttr = array_keys(Module::$moduleDictData['field_index_by_code']['attribute']['fixed']);
        $fieldsAttr = array_map(function ($item) {
            $item = "attribute." . $item;
            return $item;
        }, $fieldsAttr);

        $fields = array_merge($fieldsStepCategoryAttr, $fieldsAttr);
        $fields = build_complex_query_fields($fields, '__');

        $total = model(StepCategoryAttributeModel::class)
            ->join("attribute ON attribute.id = step_category_attribute.attribute_id", "LEFT")
            ->where($filter)->count();
        $attributeList = model(StepCategoryAttributeModel::class)
            ->join("attribute ON attribute.id = step_category_attribute.attribute_id", "LEFT")
            ->where($filter)
            ->field($fields)
            ->page($page[0], $page[1])
            ->select();

        foreach ($attributeList as &$attribute) {
            $attribute = separate_complex_query_data($attribute, '__');
        }

        return [
            'total' => $total,
            'rows' => $attributeList,
        ];
    }
}
